#include <iostream>

#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_zoomer.h>
#include <qwt_picker_machine.h>
#include <qwt_symbol.h>

#include "SymbolLine.h"
#include "PlotView.h"
#include "EndPointMarker.h"

#define gonio 1

PlotView::PlotView(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);

#ifdef gonio
	setButtonsVisibility(false);
#else
	setButtonsVisibility(true);
#endif
	
	ui.plot->setCanvasBackground(Qt::white);

	/// ustawiam pickera do rysowania punktow na wykres
	_drawPicker = new QwtPlotPicker(ui.plot->canvas());
	_pointPicker = new QwtPlotPicker(ui.plot->canvas());

	connect(ui.nextButton, SIGNAL(clicked()), this, SLOT(showNextPeak()));
	connect(ui.previousButton, SIGNAL(clicked()), this, SLOT (showPreviousPeak()));

	 _zoomer = new PlotZoomer(ui.plot->canvas());
}

void PlotView::setModel(PlotModel *model)
{
	_model = model;
	_model->setDrawPicker(_drawPicker);
	_model->setPointPicker(_pointPicker);
}

void PlotView::updateView(const bool plotWithFit)
{	
	if(!plotWithFit)
		ui.plot->detachItems();

	QPolygonF curvePoints = _model->getCurvePoints();

	QwtPlotGrid *grid = new QwtPlotGrid();
	grid->setMajorPen(Qt::black, 0, Qt::DotLine);
	grid->attach(ui.plot);
	
	QwtPlotCurve *curve = new QwtPlotCurve();
	curve->setPen(Qt::blue, 2);
	
	curve->setSamples(curvePoints);
	curve->attach(ui.plot);

	ui.plot->setAxisAutoScale(QwtPlot::yLeft);
	ui.plot->setAxisAutoScale(QwtPlot::xBottom);
	ui.plot->updateAxes();	

	_zoomer->setZoomBase();

	_zoomer->setRubberBandPen(QPen(Qt::red));

	ui.plot->replot();

	///bez tego wykres sie nie rysuje. setVisible musi miec akcje ktora odsiweza - nie moge znalezc ciala w necie
	ui.plot->setVisible(true);
}

void PlotView::updatePointPicker()
{
	_pointPicker = _model->getPointPicker();
	_pointPicker->setEnabled(false);
}

void PlotView::updateDrawPicker()
{
	/// laduje pickera do punktow, ktory zawiera wskaznik na wykres.
	_model->setDrawPicker(_drawPicker);
}

void PlotView::updatePlotMarker()
{
	QwtPlotMarker *plotMarker = _model->getPlotMarkers().back();

	plotMarker->attach(ui.plot);
	ui.plot->replot();
}

void PlotView::savePlot(const QString &fileName,const QString &filter)
{
	QString jpegExt =".jpeg", pngExt = ".png";

	/// laduje przezrocze z wykresu do pixmapy
	const QPixmap pixMap =  QPixmap::grabWidget(ui.plot); ///deprecated ; nondeprecated, ale bez osi : ui.plot->canvas()->grab();

	if(fileName.contains(jpegExt))
		pixMap.save(fileName);
	else if(fileName.contains(pngExt))
		pixMap.save(fileName);
	else
		pixMap.save(fileName + filter); /// jesli rozszerzenie nie zostalo wpisane , wybierane jest to z comboboxa
}

void PlotView::pointTrackingSettings()
{
	_pointPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,QwtPlotPicker::NoRubberBand, QwtPicker::AlwaysOn, ui.plot->canvas());
	_pointPicker->setEnabled(true);
	_model->setPointPicker(_pointPicker);
}

void PlotView::fit()
{
	updateView(true);
}

void PlotView::refreshPlot()
{
	ui.plot->replot();
}

void PlotView::showNextPeak()
{
	
}

void PlotView::showPreviousPeak()
{
}

void PlotView::setButtonsVisibility(const bool condition)
{
	ui.nextButton->setVisible(condition);
	ui.previousButton->setVisible(condition);
}
